package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.pojo.Setmeal;
import com.itheima.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    JedisPool jedisPool;

    @Override
    public void setInRedis(List<Setmeal> setmealList) {
        Jedis jedis = jedisPool.getResource();
        for (Setmeal setmeal : setmealList) {
            jedis.sadd("setmealList", setmeal.toString());
        }
    }

    public List<Setmeal> getByReids(String name) {
        ArrayList<Setmeal> setmeals = new ArrayList<>();
        Jedis jedis = jedisPool.getResource();
        Set<String> setmealList1 = jedis.smembers("setmealList");
        for (String s : setmealList1) {
//            System.out.println(s);
            String substring = s.substring(8, s.length() - 1);
//            System.out.println(substring);
            String[] split = substring.split(",");
            Setmeal setmeal = new Setmeal();
            List<String> strings = new ArrayList<>();
            for (String s1 : split) {
                String[] split1 = s1.split("=");
//                System.out.println(split1[1]);
                String substring1;
                if (split1[1].startsWith("'")) {
                    substring1 = split1[1].substring(1, split1[1].length() - 1);
//                    System.out.println(substring1);
                } else {
                    substring1 = split1[1];
                }
                strings.add(substring1);
                System.out.println(substring1);
            }
            System.out.println(strings);
            Setmeal setmeal1 = new Setmeal();
            if (!strings.get(0).startsWith("null"))
                setmeal1.setId(Integer.parseInt(strings.get(0)));
            if (!strings.get(1).startsWith("null"))
                setmeal1.setName(strings.get(1));
            if (!strings.get(2).startsWith("null"))
                setmeal1.setCode(strings.get(2));
            if (!strings.get(3).startsWith("null"))
                setmeal1.setHelpCode(strings.get(3));
            if (!strings.get(4).startsWith("null"))
                setmeal1.setSex(strings.get(4));
            if (!strings.get(5).startsWith("null"))
                setmeal1.setAge(strings.get(5));
            if (!strings.get(6).startsWith("null"))
                setmeal1.setPrice(Float.parseFloat(strings.get(6)));
            if (!strings.get(7).startsWith("null"))
                setmeal1.setRemark(strings.get(7));
            if (!strings.get(8).startsWith("null"))
                setmeal1.setAttention(strings.get(8));
            if (!strings.get(9).startsWith("null"))
                setmeal1.setImg(strings.get(9));
            setmeals.add(setmeal1);
        }
        return setmeals;
    }



    public void clearRedis(String name) {
        Jedis jedis = jedisPool.getResource();
        jedis.del(name);
    }
}
